package class555gedcom;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;


class LineMapper
{
  LineMapper(String filename)
  {
       try{

        FileInputStream fstream = new FileInputStream(filename);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        Boolean Indi = false, Family=false;
        String lasttemp = null;
        int currlvl, prevlvl;
        String currtag, prevtag;

        // reading line by line
        strLine = br.readLine();
        Boolean firsttime = false;
          while (strLine!= null)   {
              if(firsttime)
              {
              strLine = br.readLine();
              firsttime = true;
              }
 else
     firsttime = true;
            //  System.out.print(strLine);
             StringTokenizer st = new StringTokenizer(strLine);

             currlvl=Integer.parseInt(st.nextToken());
             System.out.println(currlvl);

             String temp=st.nextToken() ;
             currtag = temp;
switch(currlvl){
case 0:
if(currtag.equals("HEAD") || currtag.startsWith("@I") || currtag.startsWith("@F")|| currtag.equals("TRLR"))
                 {}
                else
                     System.out.println("tag lvl number error 0");
break;
case 1:
                 if(currtag.equals("MARR") ||currtag.equals("CHIL") ||currtag.equals("HUSB") ||currtag.equals("WIFE") ||currtag.equals("FAMS") ||currtag.equals("FAMC") ||currtag.equals("BIRT") || currtag.equals("NAME")|| currtag.equals("DEAT")|| currtag.equals("DIV")|| currtag.equals("MARR")|| currtag.equals("SEX"))
                 {

                 }
                else
                 {
                     System.out.println("tag lvl number error 10");
                 }
break;
case 2:
                 if(currtag.equals("DATE"))
                 {

                 }
                 else
                     System.out.println("tag lvl number error 2");
break;
              }
             try{
                 juhi.tags.valueOf(temp);
                  System.out.print(temp + " ");
                 // for individual tag, do these .
                 if(Indi)
                 {
                	 //System.out.println(juhi.TAG_MAP.get(lasttemp).getClass());
                	 Individual ref = (Individual) juhi.TAG_MAP.get(lasttemp);

                         // Name
                         if(temp.equals("NAME"))
                	 {
                                 String name = st.nextToken(); // for now only first name
                		 ref.setName(name);
                                 System.out.println(ref.getName());
                         }

                         // Sex - need check for sprint 1
                        else if(temp.equals("SEX"))
                	 {
                                 String sex = st.nextToken(); // sex as string
                //		 System.out.println(sex);
                                 ref.setSex(sex);
                                 System.out.println(ref.getSex());
                	 }
                         // Birth - need check for sprint 1
                        else if(temp.equals("BIRT"))
                	 {
                            strLine = br.readLine();
                            st = new StringTokenizer(strLine);
                            currlvl=Integer.parseInt(st.nextToken());

                            if(st.nextToken().equals("DATE"))
                            {
                                ref.setBirt(new Dateformat().doDate(st,currlvl));
                                System.out.println(ref.getBirt());
                            }
                	 }
                         // Death - need check for sprint 1
                        else if(temp.equals("DEAT"))
                	 {
                            strLine = br.readLine();
                            st = new StringTokenizer(strLine);
                            currlvl=Integer.parseInt(st.nextToken());
                            if(st.nextToken().equals("DATE"))
                            {
                                ref.setDeat(new Dateformat().doDate(st,currlvl));
                                System.out.println(ref.getDeat());
                            }
                	 }
                         // Divorced - need check for sprint 1
                        else if(temp.equals("DIV"))
                	 {
                            strLine = br.readLine();
                            st = new StringTokenizer(strLine);
                            currlvl=Integer.parseInt(st.nextToken());
                            if(st.nextToken().equals("DATE"))
                            {
                                ref.setDiv(new Dateformat().doDate(st,currlvl));
                                System.out.println(ref.getDiv());
                            }

                	 }
                         // FAMS
                        else if(temp.equals("FAMS"))
                	 {
                                 String fams = st.nextToken(); // fams as string
                		 ref.setFams(fams);
                                 System.out.println(ref.getFams());
                	 }
                         // FAMC
                        else if(temp.equals("FAMC"))
                	 {
                                 String famc = st.nextToken(); // famc as string
                		 ref.setFamc(famc);
                                 System.out.println(ref.getFamc());
                        }

                 }
                 // for Family tag
                 else if(Family)
                 {
                      Family ref = (Family) juhi.TAG_MAP.get(lasttemp);
                      // System.out.println(juhi.TAG_MAP.get(lasttemp).getClass());

                      // HUSB
                         if(temp.equals("HUSB"))
                	 {
                                 String husb = st.nextToken(); // husb as string
                                 ref.setHusb(husb);
                                 System.out.println(ref.getHusb());
                	 }
                      // WIFE
                        else if(temp.equals("WIFE"))
                	 {
                                 String wife = st.nextToken(); // wife as string
                                 ref.setWife(wife);
                                 System.out.println(ref.getWife());
                	 }
                       // CHILD
                        else if(temp.equals("CHIL"))
                	 {
                                 String chil = st.nextToken(); // child as string
                                 ref.setChil(chil);
                                 System.out.println(ref.getChil());
                	 }
                       // MAR
                         else if(temp.equals("MARR"))
                	 {
                            strLine = br.readLine();
                            st = new StringTokenizer(strLine);
                            currlvl=Integer.parseInt(st.nextToken());
                            if(st.nextToken().equals("DATE"))
                            {
                                ref.setMarr(new Dateformat().doDate(st,currlvl));
                                System.out.println(ref.getMarr());
                            }

                	 }

                 }

                // juhi.TAG_MAP.get(lasttemp);
                }
             catch(java.lang.IllegalArgumentException e){
            	 if(temp.startsWith("@I")){System.out.println(temp);lasttemp=temp; juhi.TAG_MAP.put(temp, new Individual()); Indi=true; Family=false;}
                 else if(temp.startsWith("@F")) {System.out.println(temp); lasttemp=temp;juhi.TAG_MAP.put(temp, new Family());Indi=false; Family=true;}
             }
catch(Exception e)
             {
                 System.out.println(e);
             }

        }
     //   strLine = br.readLine();
      }
        catch(Exception e)
        {
 System.out.print(e);
        }
      //
  }
}